--雪原信使
local xueyuanxinshi = fk.CreateSkill{
    name =  "mcdh_xueyuanxinshi",
    frequency = Skill.Compulsory,
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

xueyuanxinshi:addEffect(fk.Damage,{
    can_trigger = function(self, event, target, player, data)
        return (target == player) and player:hasSkill(self) and ( data.from and not data.from.dead and not data.to.dead and #data.from:getCardIds { Player.Hand, Player.Equip } > 0)
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local id = room:askForCardChosen(player, data.from, "he", self.name)
        player:addToPile("mcdh__xing", id, true, self.name)
    end,
})

xueyuanxinshi:addEffect(fk.Damaged,{
    can_trigger = function(self, event, target, player, data)
        return (target == player) and player:hasSkill(self) and ( data.from and not data.from.dead and not data.to.dead and #data.from:getCardIds { Player.Hand, Player.Equip } > 0)
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local id = room:askForCardChosen(player, data.from, "he", self.name)
        player:addToPile("mcdh__xing", id, true, self.name)
    end,
})

xueyuanxinshi:addEffect(fk.EventPhaseStart,{
    can_trigger = function(self, event, target, player, data)
        return (target == player) and player:hasSkill(self) and player.phase == Player.Finish
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local cmin = math.min(#player:getPile("mcdh__xing"), #room.alive_players - 1)
        local dummy = Fk:cloneCard("dilu")
        dummy:addSubcards(player:getPile("mcdh__xing"))
        room:obtainCard(player, dummy, false, fk.ReasonJustMove)
        local tos = room:askToChoosePlayers(player, {targets=room:getOtherPlayers(player),min_num=cmin,max_num=cmin,skill_name=self.name,prompt="#xueyuanxinshi-choose:::" .. cmin,cancelable=false})
        for _, p in ipairs(tos) do
            if player:isKongcheng() then
                break
            end
            if not p.dead then
                dummy:clearSubcards()
                local card = room:askForCard(player, 1, 1, true, self.name, false, ".", "#xueyuanxinshi-give:" .. p.id)
                dummy:addSubcards(card)
                room:obtainCard(p, dummy, false, fk.ReasonGive)
            end
        end
    end,
})

Fk:loadTranslationTable {
    ["mcdh__xueyuanxinshi"] = "雪原信使",
    [":mcdh__xueyuanxinshi"] = "锁定技，当你造成或受到伤害后，你将伤害来源的一张牌置于你的武将牌上，称为「信」；结束阶段，你获得所有「信」并交给等量名其他角色各一张牌。",
    ["mcdh__xing"] = "信",
    ["#xueyuanxinshi-choose"] = "雪原信使：选择交给的 %arg 名的角色",
    ["#xueyuanxinshi-give"] = "选择交给 %src 一张牌",

}

return xueyuanxinshi